package com.ayh.campusfun.service.impl;

import com.ayh.campusfun.common.PageResult;
import com.ayh.campusfun.common.service.impl.BaseServiceImpl;
import com.ayh.campusfun.common.utils.PageUtil;
import com.ayh.campusfun.dao.MessageDao;
import com.ayh.campusfun.entity.Message;
import com.ayh.campusfun.service.MessageService;
import com.ayh.campusfun.vo.MessageVo;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 消息表(Message)表服务实现类
 *
 * @author ayh
 * @since 2021-03-20 20:42:04
 */
@Service
public class MessageServiceImpl extends BaseServiceImpl<Message> implements MessageService {
    @Resource
    private MessageDao messageDao;


    @Override
    public List<MessageVo> findUnreadMessageFriendId(String uid) {
        return messageDao.findUnreadMessageFriendId(uid);
    }

    @Override
    public PageResult<Message> findMessageByFriendUid(Map<String, Object> map) {
        PageUtil.pageParamConver(map, true);
        List<Message> list  = messageDao.findMessageByFriendUid(map);
        PageInfo<Message> pageInfo = new PageInfo(list);
        Long count = messageDao.getCountByParam(map);
        Integer length = MapUtils.getInteger(map, "limit");
        int pages = (int) Math.ceil((double) count / (double) length);
        return PageResult.<Message>builder().data(pageInfo.getList()).code(0).count(count).pages(pages).build();
//        PageResult<Message> pageResult = new PageResult<>();
//        pageResult.setData(messageDao.findMessageByFriendUid(map));
//        return pageResult;
    }

    @Override
    public Long updateMessageStatus(String uid, String friendUid) {
        return messageDao.updateMessageStatus(uid, friendUid);
    }
}
